package com.yanqu.road.server.timer.thief;

import com.yanqu.road.entity.player.UserThief;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.thief.ThiefModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

/**
 * 检查小偷生成
 */
public class ThiefCheckTask extends GeneralTask {

    private long lastCheckTime = 0L;

    @Override
    public void doRun() {

        long now = System.currentTimeMillis();
        long zeroTimeTimeStamp = LocalDateTimeHelper.getZeroTimeTimeStamp();
        List<Long> refreshTimeList = StringUtils.stringToLongList(GameConfig.THIEF_REFRESH_TIME_LIST, ";");

        boolean needCheck = false;
        for (Long tmpTime : refreshTimeList) {
            long refreshTime = tmpTime * 60L * 1000L + zeroTimeTimeStamp;
            if (refreshTime > now) {
                break;
            }
            if (refreshTime > lastCheckTime) {
                needCheck = true;
                lastCheckTime = refreshTime;
                break;
            }
        }

        if (needCheck) {
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerList()) {
                ThiefModule thiefModule = player.getModule(ThiefModule.class);
                List<UserThief> thiefList = thiefModule.checkCreateThief();
                if (!thiefList.isEmpty()) {
                    thiefModule.syncThief(thiefList, 2);
                }
            }
        }

    }
}
